#include "CEUtilities.h"

#include "CEGUICheckBox.h"
#include "CEGUIRadioBoxManager.h"
#include "CEMouse.h"
#include "CEGUIPictureBox.h"
#include "CEGUILabel.h"
#include "CEGUIRadioBox.h"


CEGUIRadioBox::CEGUIRadioBox(CEGUIElementBase *parent, int id, string name, string groupname)
				:CEGUICheckBox(parent, id, name)
{
	m_eType = GUI_RadioButton;
	SetGroup(groupname);
	m_sGroupName = groupname;
	m_pLabel->SetText(groupname);
}

CEGUIRadioBox::~CEGUIRadioBox()
{
	FinalCleanUp();
}

string CEGUIRadioBox::GetGroupName()
{
	return m_sGroupName;
}


void CEGUIRadioBox::SetDefault()
{
	CEGUIRadioBoxManager::GetSingleton()->SetElementToDefault(m_sGroupName, this->GetID());
}

void CEGUIRadioBox::SetGroup(string groupname)
{
	if (m_sGroupName != groupname)
	{
		CEGUIRadioBoxManager::GetSingleton()->SetElementToGroup(groupname, this);
		m_sGroupName = groupname;
	}
}


HRESULT CEGUIRadioBox::Update(float deltaTime)
{
	if((GetButtonState() != BTS_DISABLE) && Mouse::GetSingleton()->IsMouseKeyDown(0, true) && CheckMousePosition())
	{
		CEGUIRadioBoxManager::GetSingleton()->UpdateGroup(m_sGroupName, m_nID);
	}
	return S_OK;
}

void CEGUIRadioBox::FinalCleanUp()
{
	CEGUIRadioBoxManager::GetSingleton()->RemoveElement(m_sGroupName, m_nID);	
}




void CEGUIRadioBox::SaveInScrips(ofstream &fout, int level)
{
	string blank = "";
	int temp = level;
	while (temp--) blank += "\t\t";

	fout << endl;
	fout << blank << "<element_begin>" << endl;
	fout << blank << "\t" << "<RadioBox>" << endl;

	if (m_strName != CEUtil::IntToStr(m_nID))
		fout << blank << "\t\t" << "<name>" << "\t" << m_strName << endl;
	fout << blank << "\t\t" << "<size>" << "\t" << m_Size.x << "\t" << m_Size.y << endl;
	fout << blank << "\t\t" << "<offset>" << "\t" << m_OffsetFromParent.x << "\t" << m_OffsetFromParent.y << endl;
	fout << blank << "\t\t" << "<visible>" << "\t" << m_bVisible << endl;
	fout << blank << "\t\t" << "<level>" << "\t" << m_iShowLevel << endl;
	fout << blank << "\t\t" << "<group>" << "\t" << m_sGroupName << endl;	

	CEGUIPictureBox::SaveInScrips(m_pPicNormal, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicMouseOver, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDown, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDisable, fout, level+1);

	CEGUILabel::SaveInScrips(m_pLabel, fout, level+1);

	fout << blank << "<element_end>" << endl;
}



void CEGUIRadioBox::SaveInScrips(CEGUIRadioBox* radioBox, ofstream &fout, int level)
{
	if (radioBox)
	{
		radioBox->SaveInScrips(fout, level);
	}
	else
	{
		string blank = "";
		int temp = level;
		while (temp--) blank += "\t\t";
		fout << endl;
		fout << blank << "<element_begin>" << endl;
		fout << blank << "\t" << "<RadioBox>" << endl;
		fout << blank << "\t\t" << "<NULL>" << endl;
		fout << blank << "<element_end>" << endl;
	}
}